Event handling topic
Control Events
Adding controllers to StateX objects allows those controllers to react appropriately when the app is placed in the background or when the app is terminated. It is especially important in this "Hello!" example app (see video) as a Timer is involved in the operation. Such a timer would continuously run even when its not necessary. If it was running on a mobile phone, such an app can routinely be interrupted by the user switching to another app, answering a phone call, etc. In those instances, it's very good practice to pause the app's operation whenever possible so not to be a 'memory hog'. Resume only when the user returns to the app.The StateX class has some twenty-two (22) event handling functions available to you to take into account the many external events that can affect your app. In the case of the "Hello!" example app, there is a ready means to stop the Timer when the app is placed in th background as well as start the Timer up again when the user to resume its operations. This is great news since Timers and operations like it simply take up resources.
Time to Initialise
In the screenshots below, we have the screenshot again of the Timer class being instantiated and then added to the StateX object, _HomePageState. Adding that controller now means, when the State object's initState() function is called, the initState() function in the controller, CounterTimer, is also called. Every controller added to the StateX object will have its initState() function called.The second screenshot is that of the CounterTimer class. You can see we're in its initState() where the Timer is first initialised with the initTimer() function. It's there where the Timer begins keeping tabs on the current count. When that count increments, the Timer will keep in-step.
home_page.dart | counter_timer.dart |
---|
Pause for Time
Now in the screenshots below, you see how the timer is stopped and started again depending on the circumstance. A controller class is provided an assortment of functions to address particular events. As you can readily see in the first screenshot below of the CounterTimer class, when the running app is placed in the background, for example, the pausedLifecycleState() function is called. There, the Timer is turned off. When the app is returned focus, the resumedLifecycleState() function is called and the Timer is turned on again.The second screenshot displays the didChangeAppLifecycleState() function in the StateX class. As you see, the didChangeAppLifecycleState() functions of any and all the controllers it may contain are also called. Next, however, functions dedicated to specific lifecycle events are then called. You can see where the pausedLifecycleState() function and the resumedLifecycleState() function are situated.
counter_timer.dart | state_extended.dart |
---|
Here is a further list of event-handling functions available to you:
Classes
-
StateX<
T extends StatefulWidget> Get started StateX class Using FutureBuilder Error handling Testing Event handling - The extension of the State class.
- StateXController Get started State Object Controller Testing Event handling
- Your 'working' class most concerned with the app's functionality. Add it to a 'StateX' object to associate it with that State object.
Mixins
- StateListener StateX class State Object Controller Event handling
- Responsible for the event handling in all the Controllers and State objects.